/**
 * @author tibi
 */
Ext.namespace('Softab');
Softab.EntityCreateForm = function(win, entity_tree){
    var entityForm = new Ext.FormPanel({
        id: 'myForm',
        standardSubmit: false,
        //title: 'TitluPanel',
        action: 'server/EntitySave.php',
        width: 'auto',
        height: 'auto',
        autoScroll: true,
        labelWidth: 150,
        //height			: 500,
        //width			: 700,
        isUpload: true,
        defaultType: "textfield",
		defaults: {
			anchor:'95%'
		},
        items: [{
            fieldLabel: 'Name',
            name: 'Name',
            //vtype: 'alpha',
            allowBlank: false
        }, {
            fieldLabel: 'Label',
            name: 'Label',
            //vtype: 'alphanum',
            allowBlank: false
        }, {
            fieldLabel: 'Plural Label',
            name: 'PluralLabel',
            //vtype: 'alphanum',
            allowBlank: false
        }, {
            fieldLabel: 'Entity Table',
            name: 'TableName',
            vtype: 'alphanum',
            allowBlank: false
        }/*,
		{
            xtype: "itemselector",
            name: "itemselector",
            fieldLabel: "ItemSelector",
            dataFields: ["code", "desc"],
            fromData: [[123, "One Hundred Twenty Three"], ["1", "One"], ["2", "Two"], ["3", "Three"], ["4", "Four"], ["5", "Five"], ["6", "Six"], ["7", "Seven"], ["8", "Eight"], ["9", "Nine"]],
            toData: [["10", "Ten"]],
            msWidth: 250,
            msHeight: 200,
            valueField: "code",
            displayField: "desc",
            //imagePath:"ext-ux/multiselect",
            //switchToFrom:true,
            toLegend: "Selected",
            fromLegend: "Available",
            toTBar: [{
                text: "Clear",
                handler: function(){
                    var i = entityForm.getForm().findField("itemselector");
                    i.reset.call(i);
                }
            }]
        }*/],
        buttonAlign: "center",
        buttons: [new Ext.Button({
            text: 'Salveaza',
            formBind: true,
            handler: function(){
                entityForm.getForm().doAction('submit', {
                    url: 'server/EntityCreate.php'
                })
            }
            
        })]
    
    });
    
    
    
    
    
    entityForm.startMonitoring();
    entityForm.on({
        actioncomplete: function(form, action){
            // Only enable the submit button if the load worked
            if (action.type == 'load') {
                submit.enable();
                Ext.MessageBox.alert('fs.actioncomplete', 'All OK.'); // Optional. Just for testing
            }
            
            if (action.type == 'submit') {
                entityForm.stopMonitoring();
                // If the responseText is a null string, Ext doesnt raise an error so trap it here 
                // as an error because it should be some json.
                if (action.response.responseText == '') {
                    Ext.MessageBox.alert('fs.actioncomplete error', 'Form submit returned an empty string instead of json');
                }
                else {
                    var result = Ext.decode(action.response.responseText);
                    Ext.MessageBox.alert('Result', result.msg);
                    win.close();
                    entity_tree.root.reload();
                }
            }
        },
        // The following code could be rationalised but I did it this way to make it easier
        // to follow and for you to test it yourself.
        // NOTE: the line var result = Ext.decode(action.response.responseText); must be
        // called AFTER you have made sure there wasnt a failureType == "connect" or you 
        // will probably get a JS error in the Ext library.
        
        actionfailed: function(form, action){
            //alert('actionfailed');            
            if (action.type == 'load') { // Handle the LOAD errors
                if (action.failureType == "connect") {
                    Ext.MessageBox.alert('fs.actionfailed error', 'Form load failed. Could not connect to server.');
                }
                else {
                    if (action.response.responseText != '') {
                        var result = Ext.decode(action.response.responseText);
                        if (result && result.msg) {
                            Ext.MessageBox.alert('fs.actionfailed error', 'Form load failed with error: ' + action.result.msg);
                        }
                        else {
                            Ext.MessageBox.alert('fs.actionfailed error', 'Form load failed with unknown error (possibly missing the "success" field in the json). Action type=' + action.type + ', failure type=' + action.failureType);
                        }
                    }
                    else {
                        Ext.MessageBox.alert('fs.actionfailed error', 'Form load returned an empty string instead of json');
                    }
                }
            }
            
            if (action.type == 'submit') { // Handle the SUBMIT errors
                if (action.failureType == "connect") {
                    Ext.MessageBox.alert('fs.actionfailed error', 'Form submit failed. Could not connect to server.');
                }
                else 
                    if (action.failureType == "server") {
                    // These arent "errors" as such, they are validation issues trapped by the server script and passed back for the user to correct
                    }
                    else {
                        var result = Ext.decode(action.response.responseText);
                        if (result && result.msg) {
                            Ext.MessageBox.alert('fs.actionfailed error', 'Form submit failed with error: ' + action.result.msg);
                        }
                        else {
                            Ext.MessageBox.alert('actionfailed Error', 'Form submit returned unknown error. Action type=' + action.type + ', failure type=' + action.failureType);
                        }
                    }
            }
            
        } // end actionfailed listener
    }); // end fs.on
    return entityForm;
}
